/*
 * @Description: app Mutations
 * @Author: ZY
 * @Date: 2020-12-23 10:25:37
 * @LastEditors: chenju
 * @LastEditTime: 2022-12-14 16:21:34
 */
import type { MutationTree } from 'vuex';
import type { UserState, ITree, IUserType, SideMenuItem } from './state';
import  { UserMutationTypes } from './mutation-types';
import type { RouteRecordRaw } from 'vue-router';
import type { MenuList } from '@/utils/user.utils';

export type Mutations<S = UserState> = {
  [UserMutationTypes.SET_USER_INFO](state: S, user: any): void;
  [UserMutationTypes.SET_ROLES](state: S, roles: string[]): void;
  [UserMutationTypes.SET_USER_ORGANIZATIONS](state: S, organizations: string[]): void;
  [UserMutationTypes.SHOW_LOGIN](state: S, visible: boolean): void;
  [UserMutationTypes.SET_LOGIN_STATUS](state: S, status: boolean): void;
  [UserMutationTypes.SET_ROUTES](state: S, routes: RouteRecordRaw[]): void;
  [UserMutationTypes.SET_MENU_TREE](state: S, trees: ITree[]): void;
  [UserMutationTypes.SET_TOPMENUS](state: S, trees: ITree[]): void;
  [UserMutationTypes.SET_SIDEMENUS](state: S, trees: ITree[]): void;
  [UserMutationTypes.SET_SIDEMENUS_LIST](state: S, trees: SideMenuItem[]): void;
  [UserMutationTypes.SET_PERMISSIONS](state: S, permissions: string[]): void;
  [UserMutationTypes.SET_MENU_LIST](state: S, menuList: MenuList): void;
  [UserMutationTypes.SET_USER_TYPE](state: S, userType: IUserType): void;
  [UserMutationTypes.SET_JWT_TOKEN_LOGIN_FLAG](state: S, flag: boolean): void;
};

export const mutations: MutationTree<UserState> & Mutations = {
  [UserMutationTypes.SET_USER_INFO](state: UserState, user: any) {
    state.userInfo = user;
    if (user && Object.keys(user).length) {
      localStorage.setItem('name', user.username ?? '');
      localStorage.setItem('id', user.userId ?? '');
    }
  },
  [UserMutationTypes.SET_USER_ORGANIZATIONS](state: UserState, organizations: string[]) {
    state.userOrganizations = organizations;
  },
  [UserMutationTypes.SET_ROLES](state: UserState, roles: string[]) {
    state.roles = roles;
  },
  [UserMutationTypes.SHOW_LOGIN](state: UserState, visible: boolean) {
    state.loginVisible = visible;
  },
  [UserMutationTypes.SET_ROUTES](state: UserState, routes: RouteRecordRaw[]) {
    state.routes = routes;
  },
  // 设置登录状态
  [UserMutationTypes.SET_LOGIN_STATUS](state: UserState, status: boolean) {
    state.isLogin = status;
  },
  [UserMutationTypes.SET_MENU_TREE](state: UserState, trees: ITree[]) {
    state.menuTree = trees;
  },
  [UserMutationTypes.SET_TOPMENUS](state: UserState, trees: ITree[]) {
    state.topMenus = trees;
  },
  [UserMutationTypes.SET_SIDEMENUS](state: UserState, trees: ITree[]) {
    state.sideMenus = trees;
  },
  [UserMutationTypes.SET_SIDEMENUS_LIST](state: UserState, trees: SideMenuItem[]) {
    state.sideMenuList = trees;
  },
  // 设置permission按钮权限
  [UserMutationTypes.SET_PERMISSIONS](state: UserState, permissions: string[]) {
    localStorage.setItem('permissions', JSON.stringify(permissions || []));
    state.permissions = permissions;
  },
  // 设置菜单列表
  [UserMutationTypes.SET_MENU_LIST](state: UserState, menuList: MenuList) {
    state.menuList = menuList;
  },
  // 设置用户类型
  [UserMutationTypes.SET_USER_TYPE](state: UserState, userType: IUserType) {
    state.userType = userType;
    localStorage.setItem('user-type', userType);
  },
  // 设置是否是从eps系统通过url token登录的
  [UserMutationTypes.SET_JWT_TOKEN_LOGIN_FLAG](state: UserState, flag: boolean) {
    state.isJwtTokenLoginFlag = flag;
    if (flag === true) {
      localStorage.setItem('isJwtTokenLoginFlag', 'true');
    } else {
      localStorage.removeItem('isJwtTokenLoginFlag');
    }
  }
};
